﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace HYSoft.Web
{
    public partial class macfp : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["tounit"] == null)
                uidnull();
            else
            {

                if (!IsPostBack)
                {
                    mac_band();
                    unit_list();
                    mac_list();
                    //monitor_list();
                    mac2unit();
                    
                    //clritem();
                }
                nodetype_change();
            }
        }
        protected void mac_band()
        {
            //传感器的绑定
            string sql = " [index]!=0";
            DataTable dt1 = new BLL.sensor_node().GetList(sql).Tables[0];
            if (dt1.Rows.Count > 0)
            {
                DropDownList2.DataSource = dt1.DefaultView;

                DropDownList2.DataTextField = "name";
                DropDownList2.DataValueField = "adminno";
                DropDownList2.DataBind();

                DropDownList2.Items.FindByValue("66").Selected = true;
                dt1.Clear();
                dt1.Dispose();
            }
        }

        protected void btAddOne_Click(object sender, EventArgs e)
        {
            int index = lstAll.Items.Count;
            for (int i = 0; i < index; i++)
            {
                if (lstAll.SelectedIndex == i)
                {
                    lstSelected.Items.Add(lstAll.SelectedItem);
                    lstAll.Items.RemoveAt(i);
                }
            }
        }

        protected void btnDelOne_Click(object sender, EventArgs e)
        {
            int index = lstSelected.Items.Count;
            string delmac = lstSelected.SelectedItem.Value;
            string sql = " [mac]='"+delmac+"'";
            DataTable dt = new BLL.mac_value().GetList(sql).Tables[0];
            //mac_value querymacvalue = new mac_value();
            //DataTable dt = da.chektable("mac_value", "where [mac]='" + delmac + "'");
           // DataTable dt = querymacvalue.GetList("[mac]=" + delmac).Tables[0];
            if (dt.Rows[0]["level2"].ToString() == "1")
            {
                dt.Clear();
                dt.Dispose();
                Response.Write("<script>alert('mac值" + delmac + "已被单位用户分配给传感器节点，请先联系单位用户删除对应传感器节点释放mac，然后才能回收此mac！');history.back();</script>");
            }
            else
            {
                dt.Clear();
                dt.Dispose();
                for (int i = 0; i < index; i++)
                {
                    if (lstSelected.SelectedIndex == i)
                    {
                        lstAll.Items.Add(lstSelected.SelectedItem);
                        lstSelected.Items.RemoveAt(i);
                    }
                }
            }
        }

        protected void btnAddAll_Click(object sender, EventArgs e)
        {
            int i = 0;
            foreach (ListItem item in lstAll.Items)
            {
                lstSelected.Items.Insert(i, item);
                i = i + 1;
            }
            lstAll.Items.Clear();
        }

        protected void btnDelAll_Click(object sender, EventArgs e)
        {
            int i = 0;
            string delmac = "";
            string oldmac = "";
            foreach (ListItem item in lstSelected.Items)
            {
                delmac = item.Value;
                string sql = " [mac]='" + delmac + "'";
                DataTable dt = new BLL.mac_value().GetList(sql).Tables[0];
                //mac_value querymacvalue = new mac_value();
                //DataTable dt = da.chektable("mac_value", "where [mac]='" + delmac + "'");
               // DataTable dt = querymacvalue.GetList("[mac]='" + delmac).Tables[0];
                if (dt.Rows[0]["level2"].ToString() == "1")
                {
                    dt.Clear();
                    dt.Dispose();
                    oldmac = oldmac + delmac + ",";
                }
                else
                {
                    dt.Clear();
                    dt.Dispose();
                    lstAll.Items.Insert(i, item);
                    i = i + 1;

                }

            }
            lstSelected.Items.Clear();
            if (oldmac != "")
                Response.Write("<script>alert('mac值" + oldmac + "已被单位用户分配给传感器节点，请先联系单位用户删除对应传感器节点释放mac，然后才能回收此mac！');window.location = 'macfp.aspx?tounit=" + int.Parse(Request.QueryString["tounit"].ToString()) + "&type=" + this.DropDownList2.SelectedValue + "';</script>");
        }

        private void mac_list()
        {
            int nodetype = int.Parse(DropDownList2.SelectedValue.ToString());
            string sql = " [level1]=0 and [adminno]=" + nodetype + "";
            DataTable dt = new BLL.mac_value().GetList(sql).Tables[0];
            //mac_value querymacvalue = new mac_value();
            //DataTable dt = da.chektable("mac_value", "where [level1]=0 and [adminno]=" + nodetype + "");
           // DataTable dt = querymacvalue.GetList("[level1]=0 and [adminno]=" + nodetype).Tables[0];
            lstAll.DataSource = dt.DefaultView;
            lstAll.DataTextField = "mac";//指定下拉列表中的文字显示部分 
            lstAll.DataValueField = "mac";//指定下拉列表中的值部分 
            //DataTable类实现了IListSource接口 
            lstAll.DataBind();
            dt.Clear();
            dt.Dispose();
        }

        private void mac2unit()
        {
            int nodetype = int.Parse(DropDownList2.SelectedValue.ToString());
            string sql = "[unit_index]=" + int.Parse(Request.QueryString["tounit"].ToString()) + " and [adminno]=" + nodetype + "";
            DataTable dt = new BLL.mac_value().GetList(sql).Tables[0];
           // mac_value querymacvalue = new mac_value();
            //DataTable dt = da.chektable("mac_value", "where [unit_index]=" + int.Parse(Request.QueryString["tounit"].ToString()) + " and [adminno]=" + nodetype + "");
           // DataTable dt = querymacvalue.GetList("[unit_index]=" + int.Parse(Request.QueryString["tounit"].ToString())).Tables[0];
            lstSelected.DataSource = dt.DefaultView;
            lstSelected.DataTextField = "mac";//指定下拉列表中的文字显示部分 
            lstSelected.DataValueField = "mac";//指定下拉列表中的值部分 
            //DataTable类实现了IListSource接口 
            lstSelected.DataBind();
            dt.Clear();
            dt.Dispose();



        }


        private void unit_list()
        {
            //user queryuser = new user();
            //DataTable dt2 = da2.backdatatalbeselect("select * from [user] where [type]=1 and [Intvalue2]=1 ");
            string sql = " [type]=1 and [Intvalue2]=1 ";
            DataTable dt2 = new BLL.user().GetList(sql).Tables[0];
            //DataTable dt2 = queryuser.GetList("[type]=1 and [Intvalue2]=1").Tables[0];
            if (dt2.Rows.Count > 0)
            {
                DropDownList1.DataSource = dt2.DefaultView;
                DropDownList1.DataTextField = "unit";
                DropDownList1.DataValueField = "index";
                DropDownList1.DataBind();
                dt2.Clear();
                dt2.Dispose();
                DropDownList1.SelectedValue = Request.QueryString["tounit"].ToString();
                DropDownList2.SelectedValue = Request.QueryString["type"].ToString();
            }
        }
        protected void nodetype_change()
        {
            string nodetype = DropDownList1.SelectedValue.ToString();
            if (nodetype != Request.QueryString["tounit"].ToString())
                Response.Write("<script>window.location = 'macfp.aspx?tounit=" + nodetype + "&type=" + this.DropDownList2.SelectedValue + "';</script>");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string sql = " [unit_index]=" + DropDownList1.SelectedValue.ToString() + "and [adminno]=" + this.DropDownList2.SelectedValue + "";
            DataTable dt3 = new BLL.mac_value().GetList(sql).Tables[0];
            //mac_value querymacvalue = new mac_value();
            //DataTable dt3 = da3.chektable("mac_value", "where [unit_index]=" + DropDownList1.SelectedValue.ToString() + "and [adminno]=" + this.DropDownList2.SelectedValue + "");
           // DataTable dt3 = querymacvalue.GetList("[unit_index]=" + DropDownList1.SelectedValue.ToString() + "and [adminno]=" + this.DropDownList2.SelectedValue).Tables[0];
            foreach (ListItem item in lstSelected.Items)
            {
                int flag1 = 0;
                for (int i = 0; i < dt3.Rows.Count; i++)
                {
                    if (item.Value == dt3.Rows[i]["mac"].ToString())
                        flag1 = 1;
                }
                if (flag1 == 0)
                {
                    //HYSoft.Model.mac_value macvaluemodel = querymacvalue.GetModel(Convert.ToInt32(item.Value));
                    Model.mac_value macvaluemodel = new BLL.mac_value().GetModel(Convert.ToInt32(item.Value));
                    if (macvaluemodel != null)
                    {
                        macvaluemodel.unit_index = Convert.ToInt32(this.DropDownList1.SelectedValue);
                        macvaluemodel.level1 = 1;
                        new BLL.mac_value().Update(macvaluemodel);
                        //querymacvalue.Update(macvaluemodel);
                    }
                    //da4.updatatable("mac_value", "[unit_index]='" + this.DropDownList1.SelectedValue + "',[level1]=1 where [mac]=" + item.Value + "");
                }
            }

            for (int i = 0; i < dt3.Rows.Count; i++)
            {
                int flag2 = 0;
                foreach (ListItem item in lstSelected.Items)
                {
                    if (dt3.Rows[i]["mac"].ToString() == item.Value)
                        flag2 = 1;
                }
                if (flag2 == 0)
                {
                    //HYSoft.Model.mac_value macvaluemodel = querymacvalue.GetModel(Convert.ToInt32(dt3.Rows[i]["mac"]));
                    Model.mac_value macvaluemodel = new BLL.mac_value().GetModel(Convert.ToInt32(dt3.Rows[i]["mac"]));
                    if (macvaluemodel != null)
                    {
                        macvaluemodel.unit_index = 0;
                        macvaluemodel.level1 = 0;
                        macvaluemodel.level2 = 0;
                        new BLL.mac_value().Update(macvaluemodel);
                        //querymacvalue.Update(macvaluemodel);
                    }
                    //
                    //da4.updatatable("mac_value", "[unit_index]='',[level1]=0,[level2]=0 where [mac]=" + dt3.Rows[i]["mac"] + "");
                }
            }

            Response.Write("<script>alert('更新成功');window.location = 'macfp.aspx?tounit=" + int.Parse(Request.QueryString["tounit"].ToString()) + "&type=" + this.DropDownList2.SelectedValue + "';</script>");

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Write("<script>window.location = 'macfp.aspx?tounit=" + int.Parse(Request.QueryString["tounit"].ToString()) + "&type=" + this.DropDownList2.SelectedValue + "';</script>");
        }

        private void uidnull()
        {
            //user queryuser = new user();
            //DataTable dt3 = da3.chektable("[user]", "where [type]=1 and [Intvalue2]=1 ");
            string sql = " [type]=1 and [Intvalue2]=1 ";
            DataTable dt3 = new BLL.user().GetList(sql).Tables[0];
           // DataTable dt3 = queryuser.GetList("[type]=1 and [Intvalue2]=1").Tables[0];
            if (dt3.Rows.Count > 0)
            {
                Response.Write("<script>window.location = 'macfp.aspx?tounit=" + dt3.Rows[0]["index"] + "&type=66';</script>");
            }
            else
            {
                Response.Write("<script>alert('还没有已审批的单位用户！');history.back();</script>");
            }
            dt3.Clear();
            dt3.Dispose();
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            mac_list();
            mac2unit();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            if (this.TextBox1.Text != "")
            {
                string newmac = this.TextBox1.Text;
              //  mac_value querymacvalue = new mac_value();
                string sql = " mac='" + newmac + "'";
                DataTable dt8 = new BLL.mac_value().GetList(sql).Tables[0];
                //DataTable dt8 = da8.chektable("mac_value", "where mac='" + newmac + "'");
               // DataTable dt8 = querymacvalue.GetList("mac='" + newmac + "'").Tables[0];
                if (dt8.Rows.Count > 0)
                {
                    dt8.Clear();
                    dt8.Dispose();
                    Response.Write("<script>alert('该mac值已存在，请确认手动输入的mac信息正确！');history.back();</script>");
                }
                else
                {
                    dt8.Clear();
                    dt8.Dispose();
                    //da8.insert_data("mac_value", "mac,adminno,level1,level2", "'" + newmac + "'," + this.DropDownList2.SelectedValue + ",0,0");
                     Model.mac_value macvaluemodel = new Model.mac_value();
                    macvaluemodel.mac = Convert.ToInt32(newmac);
                    macvaluemodel.adminno = Convert.ToInt32(this.DropDownList2.SelectedValue);
                    macvaluemodel.level1 = 0;
                    macvaluemodel.level2 = 0;
                    new BLL.mac_value().Add(macvaluemodel);
                    //querymacvalue.Add(macvaluemodel);
                    Response.Write("<script>alert('mac值" + this.TextBox1.Text + "手动添加成功！');window.location = 'macfp.aspx?tounit=" + int.Parse(Request.QueryString["tounit"].ToString()) + "&type=" + this.DropDownList2.SelectedValue + "';</script>");
                }
            }
            else
                Response.Write("<script>alert('手动添加mac值时，mac值输入框不能为空！');history.back();</script>");
        }

        protected void lstAll_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstSelected.SelectedIndex = -1;
        }

        protected void lstSelected_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstAll.SelectedIndex = -1;
        }

        protected void lstSelected_PreRender(object sender, EventArgs e)
        {
            //mac_value querymacvalue = new mac_value();
            for (int i = 0; i < lstSelected.Items.Count; i++)
            {
                //DataTable dt2 = da.chektable("mac_value", "where [mac]=" + lstSelected.Items[i].Value + "");
                string sql = " [mac]=" + lstSelected.Items[i].Value + "";
                DataTable dt2 = new BLL.mac_value().GetList(sql).Tables[0];
               // DataTable dt2 = querymacvalue.GetList("[mac]=" + lstSelected.Items[i].Value).Tables[0];
                if (dt2.Rows[0]["level2"].ToString() == "1")
                    lstSelected.Items[i].Attributes.Add("style ", "BACKGROUND-COLOR:   #cccccc ");
                dt2.Clear();
                dt2.Dispose();
            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
           // mac_value querymacvalue = new mac_value();
            if (lstAll.SelectedIndex == -1)
            {
                Response.Write("<script>alert('请先在\"未分配mac值\"列表中选中要删除的mac值，然后才点击\"删除\"按钮进行mac值删除！');history.back();</script>");
            }
            else
            {
                if (new BLL.mac_value().Delete(Convert.ToInt32(lstAll.SelectedValue)))
                {
                    Response.Write("<script>alert('mac值" + lstAll.SelectedValue + "删除成功！');window.location = 'macfp.aspx?tounit=" + int.Parse(Request.QueryString["tounit"].ToString()) + "&type=" + this.DropDownList2.SelectedValue + "';</script>");
                }
                else
                {
                    Response.Write("<script>alert('mac值" + lstAll.SelectedValue + "删除失败！');window.location = history;</script>");
                }
                //querymacvalue.Delete(Convert.ToInt32(lstAll.SelectedValue));
                //da.deltable("mac_value", " [mac]=" + lstAll.SelectedValue);
               
            }
        }
    }
}